@extends('admin.layout')
@section('title',$title)
@section('content')
    <div class="content-wrapper">
        <section class="content-header">
            <h1>
                商品管理
                <small>在这里，你可以浏览商品
                    @if(session('info'))
                        <span id="info" class="text-red">{{session('info')}}</span>
                    @endif
                </small>
            </h1>
            <ol class="breadcrumb">
                <li><a href="{{ url('/admin/user/index')}}"><i class="fa fa-dashboard"></i> 主页</a></li>
                <li><a href="{{ url('/admin/good/add')}}">商品管理</a></li>
                <li class="active">商品信息</li>
            </ol>
        <section class="content">
            <div class="row">
                 <div class="col-xs-12">

                    <div class="box">
                        <div class="box-header">
                            <h3 class="box-title">商品信息详情</h3>
                        </div><!-- /.box-header -->
                        <div class="box-body">
                            <table id="example2" class="table table-bordered table-hover">
                            <form action="{{ url('/admin/good/find')}}">
                                <div class="row">
                                    <div class="col-md-2">
                                        <div class="form-group">
                                            <select name="num" class="form-control">
                                            <!-- 写分页默认参数在页面上显示 -->
                                                <option
                                                    @if(!empty($request['num']) && 
                                                    $request['num'] == 5)
                                                    selected="selected"
                                                    @endif
                                                >3</option>
                                                <option
                                                    @if(!empty($request['num']) && 
                                                    $request['num'] == 10)
                                                    selected="selected"
                                                    @endif
                                                >5</option>
                                                <option
                                                    @if(!empty($request['num']) && 
                                                    $request['num'] == 20)
                                                    selected="selected"
                                                    @endif
                                                >10</option>
                                                <option
                                                    @if(!empty($request['num']) && 
                                                    $request['num'] == 50)
                                                    selected="selected"
                                                    @endif
                                                >30</option>
                                            </select>
                                        </div>
                                    </div>
                                    <div class="col-md-4 col-md-offset-6">
                                         <div class="input-group input-group">
                                                <input type="text" name="keywords" class="form-control" value="{{ $requestp['keywords'] or '' }}">
                                                    <span class="input-group-btn">
                                                      <button class="btn btn-info btn-flat" type="submit">搜索!</button>
                                                    </span>
                                            </div>
                                    </div>
                                </div>
                            </form> 
                                <thead>
                                <tr>
                                    <th>商品操作</th>
                                    <th>商品ID</th>
                                    <th>商品名称</th>
                                    <th>商品图片</th>
                                    <th>类别名</th>
                                    <th>定价</th>
                                    <th>库存</th>
                                    <th>销量</th>
                                    <th>状态</th>
                                    <th>操作</th>
                                </tr>
                                </thead>                                
                                <tbody>
                                <!-- 遍历goods数据库中的数据，到页面上显示 -->
                                @foreach($data as $row)
                                <tr>
                                    <td>dd</td>
                                    <td class="id">{{ $row -> gid}}</td>
                                    <td>{{ $row -> gname}}</td>
                                    <td><img src="{{asset($row -> gpic)}}" style="width:35px;" /></td>
                                    <td>{{ $row -> tname}}</td>
                                    <td>{{ $row -> price}}</td>
                                    <td class="td_cnt">{{ $row -> cnt}}</td>
                                    <td>{{ $row -> scnt}}</td>
                                    <td>{{ $row -> status}}</td>
                                    <td>
                                    <a href="{{ url('/admin/good/edit') }}/{{ $row -> gid }}">修改</a>
                                    <a href="{{ url('/admin/good/delete') }}/{{$row -> gid}}">删除</a>
                                    </td>
                                </tr>
                                @endforeach
                                </tbody>

                            </table>
                            <!-- 分页操作 -->
                            {{ $data -> appends($request) -> links() }}
                        </div><!-- /.box-body -->
                    </div><!-- /.box -->
                </div><!-- /.col -->
            </div><!-- /.row -->
        </section><!-- /.content -->
     </div><!-- /.content-wrapper -->

     <!-- ajax  商品库存操作-->
     <script type="text/javascript">
         
         window.onload = function()
         {
            var isDbl = true;
            $.ajaxSetup({
                headers: {
                    'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
                }
            });

            $(".td_cnt").on('dblclick', function(){

                if(!isDbl)
                {
                    return ;
                }

                isDbl = false;

                var td = $(this);

                var gid = $(this).parent().find(".id").html();

                var old_value = $(this).html();

                var inp = $('<input type="text" />');

                inp.val(old_value);

                $(this).html(inp);

                inp.select();

                inp.on('blur', function(){

                    var new_value = $(this).val();

                    $.ajax({
                        url:"{{ url('/admin/good/changeCnt')}}",
                        type:'GET',
                        data:{gid:gid,cnt:new_value},
                        success:function(data){
                            if(data == '1')
                            {
                                td.html(new_value);
                            }else
                            {
                                td.html(old_value);
                            }
                        },
                        error:function(){

                        },
                        dataType:'TEXT'
                    });

                    isDbl = true;
                });
            });

           $('#info').fadeOut(1000);
         }
     </script>
@endsection